package jiubeirobot.com.robotshop.ui.fragment.manage;

import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.fecmobile.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import jiubeirobot.com.robotshop.R;
import jiubeirobot.com.robotshop.base.BaseBean;
import jiubeirobot.com.robotshop.base.BaseFragment;
import jiubeirobot.com.robotshop.data.net.APIManager;
import jiubeirobot.com.robotshop.data.net.Flags;
import jiubeirobot.com.robotshop.listener.ItemClick;
import jiubeirobot.com.robotshop.ui.popuwindow.ListPopupWindow;
import jiubeirobot.com.robotshop.utils.PageUtils;
import jiubeirobot.com.robotshop.utils.SharedPreferencesUtils;

/**
 * ==========================
 * 创建人：TheJoker丶豪 on
 * 创建时间：2017/8/29.
 * 创建内容：提货订单的fragment 界面
 * ==========================
 */

public class ExtractOrderFragment extends BaseFragment implements XRecyclerView.LoadingListener, DrawerLayout.DrawerListener {

    @BindView(R.id.rv_extract_order_list)
    XRecyclerView xrv_saleorder;

    @BindView(R.id.rg_extract_order_list)
    RadioGroup rg_saleorder;

    @BindView(R.id.rb_extract_order_list_all)
    RadioButton rb_order_all;

    @BindView(R.id.rb_extract_order_list_unpay)
    RadioButton rb_order_unpay;

    @BindView(R.id.rb_extract_order_list_finish)
    RadioButton rb_order_finish;

    @BindView(R.id.tv_extract_order_list_select_time)
    TextView tvSelectTime;

    @BindView(R.id.fl_extract_order_detail)
    FrameLayout florderdetails;
    @BindView(R.id.dl_extract_order_list)
    DrawerLayout dl_orderlist;


    private PageUtils pageUtils = new PageUtils();
    String number;

    private ListPopupWindow listPopupWindow;
    private List<Map<String, Object>> mTimeData;

    /**
     * 筛选时间 1.全部 2.六个月 3.三个月 4.一个月 5.一个星期
     */
    private String timeSection = "1";


    @Override
    public int pageLayout() {
        return R.layout.fragment_extract_order;
    }

    @Override
    public void initTitle() {

    }

    @Override
    public void initView() {
        rg_saleorder.check(R.id.rb_extract_order_list_all);

        xrv_saleorder.setPullRefreshEnabled(true);//设置下拉刷新为TRUE
        xrv_saleorder.setLoadingMoreEnabled(false);//设置加载更多
        xrv_saleorder.setLoadingListener(this);//设置加载监听
        pageUtils.setPageSize(8);

        number = SharedPreferencesUtils.getString(getContext(), "username");
        setLoadingData();

        dl_orderlist.addDrawerListener(this);

        Drawable dividerDrawable = ContextCompat.getDrawable(getContext(), R.drawable.divider_sample);
        xrv_saleorder.addItemDecoration(xrv_saleorder.new DividerItemDecoration(dividerDrawable));
        xrv_saleorder.setLayoutManager(new LinearLayoutManager(getContext(), OrientationHelper.VERTICAL, false));

    }


    public void setLoadingData(){
        APIManager.getInstance().getExtractOrder(getContext(), number, this,pageUtils.getPageSize()+"");
    }


    @Override
    public void onSuccess(BaseBean bean, int flag) {
        switch (flag) {
            case Flags.FLAG_EXTRACT_ORDER:
                break;
            default:
                break;
        }
//        dismissHUD();
    }


    @OnClick({R.id.tv_extract_order_list_select_time, R.id.fl_extract_order_detail})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.tv_extract_order_list_select_time:
                if (listPopupWindow == null) {
                    mTimeData = new ArrayList<Map<String, Object>>();
                    String[] stringArray = getResources().getStringArray(R.array.orderTimeList);
                    for (int i = 0; i < stringArray.length; i++) {
                        HashMap<String, Object> map = new HashMap<String, Object>();
                        map.put("checked", false);
                        map.put("name", stringArray[i]);
                        map.put("postion", (i + 1));
                        mTimeData.add(map);
                    }

                    listPopupWindow = new ListPopupWindow(getContext(), mTimeData);
                    listPopupWindow.setItemClick(new ItemClick() {
                        @Override
                        public void onItemClick(int postion) {
                            timeSection = String.valueOf(mTimeData.get(postion).get("postion"));
                            tvSelectTime.setText(String.valueOf(mTimeData.get(postion).get("name")));
                            xrv_saleorder.refresh();
                        }
                    });
                }
                listPopupWindow.show(view);

                break;
            default:
                break;
        }
    }


    /**
     * recyclerview 加载监听
     */
    @Override
    public void onRefresh() {
        pageUtils.setCurentLoadMode(false);
        setLoadingData();
    }

    @Override
    public void onLoadMore() {
        pageUtils.setCurentLoadMode(true);
        setLoadingData();
    }

    @Override
    public void onDrawerSlide(View drawerView, float slideOffset) {

    }

    @Override
    public void onDrawerOpened(View drawerView) {

    }

    @Override
    public void onDrawerClosed(View drawerView) {

    }

    @Override
    public void onDrawerStateChanged(int newState) {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (listPopupWindow != null){
            listPopupWindow.dismiss(true);
        }
    }
}
